package com.yehun.utils;

import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapper;
import org.modelmapper.ModelMapper;

import java.util.Collection;
import java.util.List;

/**
 * Created by Genius on 2017/3/27 0027.
 */
public class MapperUtils {

    public static <T> T mapper(Object source, Class<T> t) {
        DozerBeanMapper mapper = new DozerBeanMapper();
        return mapper.map(source, t);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T> List<T> mapper(Collection sourceList, Class<T> t) {
        if(sourceList != null && !sourceList.isEmpty()) {
            DozerBeanMapper mapper = new DozerBeanMapper();
            List list = Lists.newArrayList();
            for (Object object : sourceList) {
                list.add(mapper.map(object, t));
            }
            return list;
        }
        return null;
    }

    public static <T> T map(Object source, Class<T> t) {
        ModelMapper mapper = new ModelMapper();
        return mapper.map(source, t);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T> List<T> map(Collection sourceList, Class<T> t) {
        if(sourceList != null && !sourceList.isEmpty()) {
            ModelMapper mapper = new ModelMapper();
            List list = Lists.newArrayList();
            for (Object object : sourceList) {
                list.add(mapper.map(object, t));
            }
            return list;
        }
        return null;
    }

}
